/*
 * @Author: xyq
 * @Date: 2021-03-02 11:40:44
 * @LastEditors: xyq
 * @LastEditTime: 2021-03-05 11:50:22
 * @FilePath: \little-eight\src\utils\request.js
 */
import axios from 'axios';
import { getToken } from '../service/auth';
const instance = axios.create({
  baseURL: '/api',
  timeout: 50000,
});
instance.defaults.headers = {
  Authorization: 'Basic cGxhdGZvcm1fbGljZW5zZV9hZG1pbjpod192ZGlfbGljZW5zZQ==',
};
//请求发出前拦截
instance.interceptors.request.use(
  config => {
    // 是否需要设置 token
    console.log(getToken());
    const isToken = (config.headers || {}).isToken === false;
    if (getToken() && !isToken) {
      config.headers['Authorization'] = 'Bearer ' + getToken(); // 让每个请求携带自定义token 请根据实际
      // config.headers['TERMINAL'] = 'WEB';
    }
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
);
//请求响应拦截
instance.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);

export function get(url, params) {
  return instance.get(url, {
    params,
  });
}

export function post(url, data) {
  return instance.post(url, data);
}
